//create by lwy 2017年9月27日11:45:10
var page =0;
var orderNum = 0;
var zuoDanStateValue = GetQueryString('zuoDanStateValue');

$(function () {
    if(zuoDanStateValue==""||zuoDanStateValue==undefined||zuoDanStateValue==null){
        zuoDanStateValue=''
    }else{
        zuoDanStateValue = GetQueryString('zuoDanStateValue');
        $('.swiper-slide[stateValue='+zuoDanStateValue+']').addClass('active');
        $('.swiper-slide[stateValue='+zuoDanStateValue+']').siblings().removeClass('active');
    }
    doAjax();
})

function doAjax() {
    page++;
    $.ajax({
        url:'/zuoDanC/userPurchaseOrderList.json',
        dataType:'json',
        data:{
            pageNu:page,
            token:getItem('token'),
            zuoDanStateValue:zuoDanStateValue,
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.order_box'));
            }
        },
        success:function (data) {
            if(data.code==1){
                if(page==1){
                    $('.order_box').empty();
                    if(data.datas.zuoDanList.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('.order_box'));
                    }
                }
                var shopData = data.datas.zuoDanList;
                var z = '';
                for(var j=0;j<shopData.length;j++){
                    $('<div class="order" zuodanId="'+shopData[j].zuoDanId+'" id="shop'+shopData[j].zuoDanId+'">' +
                        '<div class="shopName"><span>'+shopData[j].shopName+'</span>' +
                        '<span class="mui-icon mui-icon-arrowright"></span>' +
                        '<span class="state">'+shopData[j].state+'</span>' +
                        '</div></div>').appendTo($('.order_box'))
                    var goodsData = shopData[j].goodsOrderList;
                    var hasComment = '';
                    for(var i=0;i<goodsData.length;i++){
                        z = goodsData.length;
                        hasComment = goodsData[i].hasComment;
                        $('<div class="detailBox mui-clearfix">\n' +
                            '<div class="order-img-box">\n' +
                            '<img src="'+goodsData[i].picture+'">\n' +
                            '</div>\n' +
                            '<div class="order-product-box">\n' +
                            '<p class="orderName">'+goodsData[i].goodsName+'</p>\n' +
                            '<span class="guige">'+goodsData[i].goodsSpecification+'</span>\n' +
                            '<span class="price">￥'+goodsData[i].salePrice+'<i>×'+goodsData[i].num+'</i></span>\n' +
                            '</div>\n' +
                            '</div>').appendTo($('#shop'+shopData[j].zuoDanId+''))
                    }
                    $('<div class="total">\n' +
                        '<span class="total_price">小计： <i>¥ '+shopData[j].z_shopYingYeMoney+'</i></span>\n' +
                        '<span class="total_count">共'+z+'件商品</span>\n' +
                        '</div>' +
                        '<div id="handleBox'+shopData[j].zuoDanId+'" zuodanId="'+shopData[j].zuoDanId+'"></div>'+
                        '<div class="line"></div>').appendTo($('.order_box'))
                    //console.log(shopData[j].stateValue);
                    if(shopData[j].stateValue==4){
                        $('<div class="handle_order">' +
                            '<a class="blue_handle cancleOrder">取消订单</a></div>').appendTo($('#handleBox'+shopData[j].zuoDanId+''));
                    }
                    if(shopData[j].stateValue==8||shopData[j].stateValue==1||shopData[j].stateValue==7){
                        $('<div class="handle_order">' +
                            '<a class="gray_handle delOrder">删除订单</a></div>').appendTo($('#handleBox'+shopData[j].zuoDanId+''));
                    }
                    if(shopData[j].stateValue==5){
                        $('<div class="handle_order">' +
                            '<a class="blue_handle certainOrder">确认收货</a></div>').appendTo($('#handleBox'+shopData[j].zuoDanId+''));
                    }
                    if(shopData[j].stateValue==2){
                        var delOrder = '';
                        if(hasComment){
                            delOrder = ''
                        }else{
                            delOrder = '<a class="blue_handle commentOrder">立即评价</a>'
                        }
                        $('<div class="handle_order">' +
                            ''+delOrder+'<a class="gray_handle delOrder">删除订单</a></div>').appendTo($('#handleBox'+shopData[j].zuoDanId+''));
                    }
                }
                orderNum = shopData.length;
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
    return orderNum;
}

//滑动导航
var mySwiper = new Swiper('#topNav', {
    freeMode: true,
    freeModeMomentumRatio: 0.5,
    slidesPerView: 'auto',

});

swiperWidth = mySwiper.container[0].clientWidth
maxTranslate = mySwiper.maxTranslate();
maxWidth = -maxTranslate + swiperWidth / 2

$(".swiper-container").on('touchstart', function(e) {
    e.preventDefault()
})

mySwiper.on('tap', function(swiper, e) {

//	e.preventDefault()

    slide = swiper.slides[swiper.clickedIndex]
    slideLeft = slide.offsetLeft
    slideWidth = slide.clientWidth
    slideCenter = slideLeft + slideWidth / 2
    // 被点击slide的中心点

    mySwiper.setWrapperTransition(300)

    if (slideCenter < swiperWidth / 2) {

        mySwiper.setWrapperTranslate(0)

    } else if (slideCenter > maxWidth) {

        mySwiper.setWrapperTranslate(maxTranslate)

    } else {

        nowTlanslate = slideCenter - swiperWidth / 2

        mySwiper.setWrapperTranslate(-nowTlanslate)

    }

    $("#topNav  .active").removeClass('active')

    $("#topNav .swiper-slide").eq(swiper.clickedIndex).addClass('active');
    zuoDanStateValue = $("#topNav .swiper-slide").eq(swiper.clickedIndex).attr('stateValue');
    console.log(zuoDanStateValue);
    mui('#refreshContainer').pullRefresh().scrollTo(0, 0);//滚动到顶部
    mui('#refreshContainer').pullRefresh().refresh(true);
    if($('.mui-pull-caption').html()=='上拉显示更多'){
        $('.mui-pull-caption').html('');
    }
    $('.order_box').empty();
    page=0;
    doAjax();

})

//取消订单
mui('body').on('tap','.cancleOrder',function () {
    var cancleOrderId = $(this).parent().parent().attr('zuodanId');
    var btnArray = ['取消', '确定'];
    mui.confirm('您确认要取消此订单？', '', btnArray, function(e) {
        if (e.index == 1) {
            $.ajax({
                url:'/zuoDanC/cancelOrder.json',
                dataType:'json',
                data:{
                    token:getItem('token'),
                    zuoDanId:cancleOrderId,
                },
                type:'post',
                beforeSend:function(){
                    $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                },
                success:function (data) {
                    $('.loading2').hide();
                    if(data.code==1){
                        mui.toast('取消订单成功');
                        window.location.reload();
                    }
                    if(data.code==-1){
                        window.location.href="login.html"
                    }
                    if(data.code==0){
                        mui.toast(data.msg);
                        return;
                    }
                }
            })
        } else {

        }
    })
})

//确认收货
mui('body').on('tap','.certainOrder',function () {
    var cancleOrderId = $(this).parent().parent().attr('zuodanId');
    var btnArray = ['取消', '确定'];
    mui.confirm('请确认已收到货之后再确认订单？', '', btnArray, function(e) {
        if (e.index == 1) {
            $.ajax({
                url:'/zuoDanC/receiptOrder.json',
                dataType:'json',
                data:{
                    token:getItem('token'),
                    zuoDanId:cancleOrderId,
                },
                type:'post',
                beforeSend:function(){
                    $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                },
                success:function (data) {
                    $('.loading2').hide();
                    if(data.code==1){
                        mui.toast('确认收货成功');
                        window.location.reload();
                    }
                    if(data.code==-1){
                        window.location.href="login.html"
                    }
                    if(data.code==0){
                        mui.toast(data.msg);
                        return;
                    }
                }
            })
        } else {

        }
    })
})

//删除订单
mui('body').on('tap','.delOrder',function () {
    var cancleOrderId = $(this).parent().parent().attr('zuodanId');
    var btnArray = ['取消', '确定'];
    mui.confirm('您确认要删除此订单？', '', btnArray, function(e) {
        if (e.index == 1) {
            $.ajax({
                url:'/zuoDanC/delZuoDan.json',
                dataType:'json',
                data:{
                    token:getItem('token'),
                    zuoDanId:cancleOrderId,
                },
                type:'post',
                beforeSend:function(){
                    $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                },
                success:function (data) {
                    $('.loading2').hide();
                    if(data.code==1){
                        mui.toast('删除订单成功')
                        window.location.reload();
                    }
                    if(data.code==-1){
                        window.location.href="login.html"
                    }
                    if(data.code==0){
                        mui.toast(data.msg);
                        return;
                    }
                }
            })
        } else {

        }
    })
})

//评价
mui('body').on('tap','.commentOrder',function () {
    var cancleOrderId = $(this).parent().parent().attr('zuodanId');
    window.location.href="doComment_v2.html?zuoDanId="+cancleOrderId;
})

//订单详情
mui('body').on('tap','.order',function () {
    var zuoDanId = $(this).attr('zuodanid')
    window.location.href="orderDetail_v2.html?zuoDanId="+zuoDanId;
})

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();
                    if (numResult < 10) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

$('#outline').click(function () {
    window.location.href="userOutlineOrder_v2.html"
})
